<template>
  <div class="container" v-loading="isLoading">
    <el-card>
      <template #header>
        <el-form inline :model="searchForm" ref="searchForm">
          <el-form-item label="小区">
            <el-select v-model="searchForm.subdistrict">
              <el-option label="全部" value="" />
              <el-option
                v-for="(oVal, oIdx) of communityType"
                :key="oIdx"
                v-bind="oVal"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="searchForm.status">
              <el-option label="全部" value="" />
              <el-option
                v-for="(oVal, oIdx) of status"
                :key="oIdx"
                v-bind="oVal"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitSearch">查询</el-button>
          </el-form-item>
        </el-form>
        <div class="table_tool">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="showAddOrEdit = true"
            >新增</el-button
          >
        </div>
      </template>
      <el-table :data="tableData" border>
        <el-table-column prop="title" label="标题" />
        <el-table-column prop="subdistrict" label="小区">
          <template slot-scope="{ row }">
            {{ communityObj[row.subdistrict] }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="{ row }">
            <span :class="statusClass[row.status]">{{
              statusObj[row.status]
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" />
        <el-table-column label="操作" width="120px" align="center">
          <template slot-scope="{ row }">
            <el-button type="text" @click="openEditRow(row, 'builingForm')"
              >编辑</el-button
            >
            <el-button type="text" class="red" @click="delRowById(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <footer class="footer">
      <el-pagination
        background
        layout="total, sizes, pager, jumper"
        :current-page="pagination.page"
        :page-sizes="[10, 20, 30, 50, 100]"
        :page-size="pagination.page_size"
        :total="tableTotal"
        @size-change="(val) => (pagination.page_size = val)"
        @current-change="(val) => (pagination.page = val)"
      />
    </footer>

    <el-dialog
      :title="builingForm.$id ? '编辑' : '新增'"
      :visible.sync="showAddOrEdit"
      width="740px"
      @closed="() => $refs['builingForm'].resetFields()"
    >
      <el-form
        :model="builingForm"
        :rules="builingRules"
        ref="builingForm"
        label-width="6em"
      >
        <el-form-item prop="$id" hidden />
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="builingForm.title"
            placeholder="请输入名称"
          ></el-input>
        </el-form-item>
        <el-form-item class="span-2" label="小区" prop="subdistrict">
          <el-select v-model="builingForm.subdistrict">
            <el-option
              v-for="(oVal, oIdx) of communityType"
              :key="oIdx"
              v-bind="oVal"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="span-2" label="状态" prop="status">
          <el-select v-model="builingForm.status">
            <el-option
              v-for="(oVal, oIdx) of status"
              :key="oIdx"
              v-bind="oVal"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注内容" prop="text">
          <el-input
            v-model="builingForm.text"
            type="textarea"
            row="6"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button
          type="primary"
          @click="submitRowData('builingForm')"
          :loading="loading.update"
          >确 定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { table, addOrEdit, status } from "@/utils/mixins";
import API from "@/api";
export default {
  mixins: [table, addOrEdit, status],
  data: () => ({
    searchForm: {
      $action: ["getMultiportList", "delMultiport"],
      subdistrict: "",
      status: "",
    },
    builingForm: {
      $action: ["addMultiport", "editMultiport"],
      title: "",
      subdistrict: "",
      status: "",
      text: "",
    },
    builingRules: {},

    communityType: [],
    status: [
      { label: "上线", value: 1 },
      { label: "下线", value: 2 },
    ],
  }),
  computed: {
    communityObj() {
      return this.communityType.reduce(
        (acc, cur) => ((acc[cur.value] = cur.label), acc),
        {}
      );
    },
  },
  created() {
    API.getCommunityList({ page_size: 100 })
      .then(({ data }) => {
        this.communityType = data.map((v) => ({
          label: v.name,
          value: v.id,
        }));
      })
      .catch((err) => {});
  },
  methods: {},
};
</script>

<style lang="scss" scoped></style>
